
import React from "react";
import Controller from "../../../../Controller";

export default function useTransformControls(controller: Controller) {

    const scene = controller.useMemo(s => s.scene, []);

    const selectedElementUuid = controller.useMemo(s => s.selectedElementUuid, []);

    const transformControls = controller.useMemo(s => s.transformControls, []);

    const selectedTool = controller.useMemo(s => s.selectedTool, []);

    React.useEffect(() => {
        if (!scene || !transformControls) return;
        
        const selectedElement = scene.children.filter(e => e.uuid === selectedElementUuid)[0];
        if (selectedTool === 'position' && selectedElement) {
            transformControls.attach(selectedElement).setMode("translate");
            scene.add(transformControls);
        } else if (selectedTool === 'orientation' && selectedElement) {
            transformControls.attach(selectedElement).setMode("rotate");
            scene.add(transformControls);
        }else if (selectedTool === 'scale' && selectedElement) {
            transformControls.attach(selectedElement).setMode("scale");
            scene.add(transformControls);
        } else {
            scene.remove(transformControls.detach());
        }
    }, [scene, selectedElementUuid, selectedTool, transformControls])
}

